<?php
/**
 * ics_show Plugin Helper:
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Stephane Chamberland <dokuwiki@meteokid.ca>
 * @date       2009-02-22
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
if(!defined('DOKU_PLUGIN_ICS_SHOW')) define('DOKU_PLUGIN_ICS_SHOW',DOKU_PLUGIN.'icsshow/');

class helper_plugin_icsshow extends DokuWiki_Plugin {

    //minimal number of args for each type [+1]
    var $argcheck = array(
                        );

    /**
     *
     */
    function getInfo() {
        return array(
                'author' => 'Stephane Chamberland',
                'email'  => 'dokuwiki@meteokid.ca',
                'date'   => '2009-02-22',
                'name'   => 'Ics Show Plugin (helper class)',
                'desc'   => 'Display remote ICS calendar events',
                'url'    => 'http://wiki.splitbrain.org/plugin:ics_show',
                );
    }

    /**
     *
     */
    function getMethods() {
        $result = array();
        $result[] = array(
                'name'   => '_render_xhtml',
                'desc'   => 'Render Calendar as xhtml',
                'params' => array(
                    'cals_url' => ')array',
                    'options' => 'array'
                    ),
                'return' => array('out' => 'string'),
                );
       //TODO: update
       return $result;
    }
    /**
     * Create output
     */
    function _render_xhtml($cals_url,$options0,$renderObj=false) {
        global $ID;
        global $lang;
        //TODO: check options and fill with plugin options.
        //TODO: make sure cal names do not clash with filename (add ics_show__ prefix)
        //TODO: if format is template file... read it
        //TODO: wl for control links
        require_once(DOKU_PLUGIN_ICS_SHOW.'ACC_Calendar.class.php');
        //TODO: check if cache_dir exist and is writable
        $options = array();
        foreach(array('date_min', 'date_max', 'date0', 'sort_order', 'shortdescsep', 'shortdesclen', 'format', 'dateformat', 'nb_item_page', 'formatlong', 'formattags', 'cache_timeout', 'allow_markup','show_controls','prefix','postfix','linktopage') as $k){
            $options[$k] = $this->getConf($k);
            if(isset($options0[$k]) && $options0[$k][1]) $options[$k] = $options0[$k][1];
        }

        $cache_dir = metaFN($this->getConf('cache_dir'),'/');

        $mycal = new ACC_Calendar_HTML($cache_dir,$cache_dir,$options['cache_timeout']);

        $mycal->set_sort_order($mycal->old2new);
        if($options['sort_order'] == 'new2old') $mycal->set_sort_order($mycal->new2old);
        //msg('range:'.$options['date_min'].$options['date_max'],0);
        $mycal->set_range($options['date_min'],$options['date_max']);
        $mycal->setShortDescLen($options['shortdesclen'],$options['shortdescsep']);

        $linktopage = $options['linktopage'];
        if(!$linktopage) $linktopage = $ID;
        resolve_pageid(getNS($ID),$linktopage,$exists);

        if(!isset($options['formatlong'])) $options['formatlong'] = $options['format'];
        $defaultTagsVals = array();
        foreach(explode(" ",$options['formattags']) as $opt) {
            if(trim($opt)) {
                list($k,$v) = explode(":",$opt,2);
                $defaultTagsVals[$k] = str_replace(":"," ",$v);
            }
        }

        foreach(array('ics_page','ics_id') as $k){
            $options[$k] = 0;
            //TODO: clean post/get values
            if(isset($_POST[$k])) $options[$k] = $_POST[$k];
            if(isset($_GET[$k])) $options[$k] = $_GET[$k];
        }
        $format = $options['format'];
        $display_page_mode = true;
        if($options['ics_id']) {
            $display_page_mode = false;
            $format = $options['formatlong'];
        }


        $uidtag = '____uidtag____';
        $uidtag2 = '@@uid@@';
        $more_link = wl($linktopage,array('ics_id'=>$uidtag));
        $controls = '';
        if($options['show_controls']) {
            //TODO: localize
            //$lang['btn_icsshow_more'] = 'more details >>';
			$lang['btn_icsshow_more'] = $this->getLang('btn_icsshow_more');
            $more_btn = '<div class="more">';
            $more_btn .= html_btn('icsshow_more',$linktopage,'o',array('ics_id'=>$uidtag), 'post','More');
            $more_btn .= '</div>';
            //$defaultTagsVals['+uid'] = 'myuid';
        }

        $format = str_replace(array('@@more_link@@','@@more_btn@@',hsc($uidtag)),array($more_link,$more_btn,$uidtag),$format);
        $format = str_replace($uidtag,$uidtag2,$format);
        $mycal->setFormat($format,$defaultTagsVals);
        $mycal->setDateFormat($options['dateformat']);
        if($renderObj && $options['allow_markup']) $mycal->setRenderObj($renderObj);
        $mycal->addCals($cals_url);
        //TODO: may want to check GET options adn set page,date or id accordingly

        $controls = '';
        if($options['show_controls']) {
            //TODO: localize
            //$lang['btn_icsshow_list'] = 'List';
            //$lang['btn_icsshow_prev'] = '<< Prev';
            //$lang['btn_icsshow_next'] = 'Next >>';
            $lang['btn_icsshow_list'] = $this->getLang('btn_icsshow_list');
            $lang['btn_icsshow_prev'] = $this->getLang('btn_icsshow_prev');
            $lang['btn_icsshow_next'] = $this->getLang('btn_icsshow_next');

            if($display_page_mode){
                if($options['sort_order']=='old2new'){
                    list($prev,$next) = $mycal->hasPrevNextPage($options['date0'],$options['nb_item_page'],$options['ics_page']);
                    $btn_prev = html_btn('icsshow_prev',$ID,'p',
                        array('ics_page' => $options['ics_page']-1),'post','Previous');
                    $btn_next = html_btn('icsshow_next',$ID,'n',
                        array('ics_page' => $options['ics_page']+1),'post','Next');
                } else {
                    list($next,$prev) = $mycal->hasPrevNextPage($options['date0'],$options['nb_item_page'],$options['ics_page']);
                    $btn_prev = html_btn('icsshow_prev',$ID,'p',
                        array('ics_page' => $options['ics_page']+1),'post','Previous');
                    $btn_next = html_btn('icsshow_next',$ID,'n',
                        array('ics_page' => $options['ics_page']-1),'post','Next');
                }
                $btn_list = '';
            } else {
                if($options['sort_order']=='old2new'){
                    list($prev,$next) = $mycal->getPrevNextItemId($options['ics_id']);
                    $btn_prev = html_btn('icsshow_prev',$ID,'p',
                        array('ics_id' => $prev),'post','Previous');
                    $btn_next = html_btn('icsshow_next',$ID,'n',
                        array('ics_id' => $next),'post','Next');
                } else {
                    list($next,$prev) = $mycal->getPrevNextItemId($options['ics_id']);
                    $btn_prev = html_btn('icsshow_prev',$ID,'p',
                        array('ics_id' => $prev),'post','Previous');
                    $btn_next = html_btn('icsshow_next',$ID,'n',
                        array('ics_id' => $next),'post','Next');
                }
                $btn_list = html_btn('icsshow_list',$ID,'l',
                    array('ics_page' => 0),'post','List');
            }

            $controls = '<div class="more"><br/>';
            if($prev) $controls .= $btn_prev;
            $controls .= '&nbsp;'.$btn_list.'&nbsp;';
            if($next) $controls .= $btn_next;
            $controls .= '<br /></div>';
        }
        if($display_page_mode){
            $msg = $mycal->toStr($options['date0'],$options['nb_item_page'],$options['ics_page']);
        } else {
            $msg = $mycal->toStrItem($options['ics_id']);
        }
        return $options['prefix'].$controls.$msg.$controls.$options['postfix'];
    }

}



// vim:ts=4:sw=4:et:enc=utf-8:
